package kgc.NewsDaoImpl;

import kgc.NewsDao.NewsDao;
import kgc.pojo.News_Category;
import kgc.pojo.News_Comment;
import kgc.pojo.News_Detail;
import kgc.pojo.News_User;
import kgc.util.BaseDao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author DXH
 * @version 1.0.0
 * @ClassName NewsDaoImpl
 * @Description TODO
 * @createTime 2022年04月05日 12:28:35
 */
public class NewsDaoImpl extends BaseDao implements NewsDao {
    @Override
    public List<News_Category> selectCategoryAll() {
        String sql = "SELECT * FROM `news_category`";
        Object[] objects = {};
        ResultSet resultSet = null;
        List<News_Category> list = new ArrayList<News_Category>();
        try {
            resultSet = this.selectObj(sql, objects);
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Date createDate = resultSet.getDate("createDate");
                News_Category category = new News_Category();
                category.setId(id);
                category.setName(name);
                category.setDate(createDate);
                list.add(category);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Statement statement =null;
            Connection connection=null;
            this.closeConnection(resultSet, preparedStatement, statement, connection);
        }
        return list;
    }

    @Override
    public List<News_Comment> selectCommentAll() {
        String sql = "SELECT * FROM `news_comment`";
        Object[] objects = {};
        ResultSet resultSet = null;
        List<News_Comment> list = new ArrayList<News_Comment>();
        try {
            resultSet = this.selectObj(sql, objects);
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                int newsId = resultSet.getInt("newsId");
                String content = resultSet.getString("content");
                String author = resultSet.getString("author");
                String ip = resultSet.getString("ip");
                Date createDate = resultSet.getDate("createDate");
                News_Comment comment = new News_Comment();
                comment.setId(id);
                comment.setNewsId(newsId);
                comment.setContent(content);
                comment.setContent(ip);
                comment.setDate(createDate);
                list.add(comment);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Statement statement =null;
            Connection connection=null;
            this.closeConnection(resultSet, preparedStatement, statement, connection);
        }
        return list;
    }

    @Override
    public List<News_Detail> selectDetailAll() {
        String sql = "SELECT * FROM `news_detail`";
        Object[] objects = {};
        ResultSet resultSet = null;
        List<News_Detail> list = new ArrayList<News_Detail>();
        try {
            resultSet = this.selectObj(sql, objects);
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                int categoryId = resultSet.getInt("categoryId");
                String title = resultSet.getString("title");
                String summary = resultSet.getString("summary");
                String picPath = resultSet.getString("picPath");
                String author = resultSet.getString("author");
                Date createDate = resultSet.getDate("createDate");
                Date modifyDate = resultSet.getDate("modifyDate");
                News_Detail detail = new News_Detail();
                detail.setId(id);
                detail.setCategoryId(categoryId);
                detail.setTitle(title);
                detail.setSummary(summary);
                detail.setPicPath(picPath);
                detail.setAuthor(author);
                detail.setCreateDate(createDate);
                detail.setModifyDate(modifyDate);
                list.add(detail);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Statement statement =null;
            Connection connection=null;
            this.closeConnection(resultSet, preparedStatement, statement, connection);
        }
        return list;
    }

    @Override
    public List<News_User> selectUserAll() {
        String sql = "SELECT * FROM `news_user`";
        Object[] objects = {};
        ResultSet resultSet = null;
        List<News_User> list = new ArrayList<News_User>();
        try {
            resultSet = this.selectObj(sql, objects);
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                String userName = resultSet.getString("userName");
                String password = resultSet.getString("password");
                String email = resultSet.getString("email");
                int userType = resultSet.getInt("userType");
                News_User user = new News_User();
                user.setId(id);
                user.setUserName(userName);
                user.setPassword(password);
                user.setEmail(email);
                user.setUserType(userType);
                list.add(user);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            PreparedStatement preparedStatement=null;
            Statement statement =null;
            Connection connection=null;
            this.closeConnection(resultSet, preparedStatement, statement, connection);
        }
        return list;
    }

    @Override
    public int deleteById(int id, int num) {
        String sql = null;
        if (num ==1){
            sql = "DELETE FROM `news_category` WHERE id=?";
        }else if (num ==2){
            sql = "DELETE FROM `news_user` WHERE id=?";
        }
        Object[] objects = {id};
        int rows = 0;
        try {
            rows = this.updateObj(sql, objects);
        }catch (Exception e){
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public int updateById(int id, int num) {
        int rows = 0;
        String sql = null;
        if (num == 1){
            sql = "UPDATE `news_category` SET `name`=?,`createDate`=? WHERE id =?";
            News_Category category = new News_Category();
            Object[] objects = {category};
            rows = this.updateCategory(sql, objects);
        }else if (num == 2){
            sql = "UPDATE `news_user` SET userName = ?,`password`=?,`email`=?,`userType`=? WHERE id =?";
            News_User user = new News_User();
            Object[] objects = {user};
            rows = this.updateUser(sql, objects);
        }
        try {

        }catch (Exception e){
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public int updateCategory(News_Category category) {
        int rows = 0;
        String sql = "UPDATE `news_category` SET `name`=?,`createDate`=? WHERE id =?";
        Object[] objects = {category};
        rows = this.updateCategory(sql, objects);
        return rows;
    }

    @Override
    public int updateUser(News_User user) {
        int rows = 0;
        String sql = "UPDATE `news_user` SET userName = ?,`password`=?,`email`=?,`userType`=? WHERE id =?";
        Object[] objects = {user};
        rows = this.updateUser(sql, objects);
        return rows;
    }

    @Override
    public ResultSet selectById(int id, int num) {
        String sql = null;
        ResultSet resultSet = null;
        if (num == 1){
            sql = "SELECT * FROM `news_category` where id=?";
            Object[] objects = {id};
            resultSet = this.selectObj(sql, objects);
        }else if (num == 2){
            sql = "SELECT * FROM `news_user` where id=?";
            Object[] objects = {id};
            resultSet = this.selectObj(sql, objects);
        }
        return resultSet;
    }

    @Override
    public int insertCategory(News_Category category) {
        int rows = 0;
        String sql = "INSERT INTO `news_category`(id,name,createdate) VALUES(?,?,?)";
        Object[] objects = {category};
        rows = this.addCategory(sql, objects);
        return rows;
    }

    @Override
    public int insertUser(News_User user) {
        int rows = 0;
        String sql = "INSERT INTO `news_user`(`id`,`userName`,`password`,`email`,`userType`) VALUES(?,?,?,?,?)";
        Object[] objects = {user};
        rows = this.addUser(sql, objects);
        return rows;
    }
}
